package controllers;

import java.util.Date;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import services.ChallengeService;
import services.EventService;

import domain.Challenge;
import domain.Event;

@Controller
@RequestMapping("/challenge/administrator")
public class ChallengeAdministratorController extends AbstractController {
	
	// Constructors -----------------------------------------------------------

	public ChallengeAdministratorController() {
		super();
	}

	// Services ---------------------------------------------------------------

	@Autowired
	private ChallengeService challengeService;
	
	@Autowired
	private EventService eventService;


	// Creation ---------------------------------------------------------------

	@RequestMapping(value = "/create", method = RequestMethod.GET)
	public ModelAndView create(@RequestParam int eventId) {
		ModelAndView result;
		Challenge challenge;
		
		Event e = eventService.findOne(eventId);
		challenge = challengeService.create(e);
		System.out.println("Llega a crear callengue");
		result = createEditModelAndView(challenge);
		result.addObject("create", true);
		result.addObject("eventId", challenge.getEvent().getId());

		return result;
	}

	protected ModelAndView createEditModelAndView(Challenge challenge) {
		ModelAndView result;

		result = createEditModelAndView(challenge, null);

		return result;
	}

	protected ModelAndView createEditModelAndView(Challenge challenge, String message) {
		ModelAndView result;
		result = new ModelAndView("challenge/edit");
		result.addObject("challenge", challenge);
		if(challenge.getId()==0)
		result.addObject("create", true);
		result.addObject("message", message);
		return result;
	}

	// Edit ---------------------------------------------------------------
	
			@RequestMapping(value = "/edit", method = RequestMethod.GET)
			public ModelAndView editChallenge(@RequestParam int challengeId) {
				ModelAndView result;
				Challenge challenge = challengeService.findOne(challengeId);
				Event event = challenge.getEvent();
				result = createEditModelAndView(challenge);
				result.addObject("readonly", !(event.getStartMoment().compareTo(new Date())>0));
				result.addObject("eventId", event.getId());
				return result;
			}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "save")
	public ModelAndView save(@Valid Challenge challenge, BindingResult binding) {
		ModelAndView result;

		if (binding.hasErrors()&&!challengeService.itsHacked(challenge)) {
			
			result = createEditModelAndView(challenge);
			
		} 
		else if(challengeService.itsHacked(challenge))
		{
			result = createEditModelAndView(challenge,"challenge.commit.error");
		}
		else {
			try {
				result = persist(challenge);
			} catch (Throwable oops) {
				result = createEditModelAndView(challenge, "challenge.commit.error");
			}
		}

		return result;
	}

	@RequestMapping(value = "/edit", method = RequestMethod.POST, params = "delete")
	public ModelAndView delete(Challenge challenge, BindingResult binding) {
		ModelAndView result;

		try {
			challengeService.delete(challenge);
			result = new ModelAndView("redirect:/challenge/list.do?eventId="+challenge.getEvent().getId());
		} catch (Throwable oops) {
			result = createEditModelAndView(challenge, "challenge.commit.error");
		}

		return result;
	}

	private ModelAndView persist(Challenge c){
		ModelAndView result;
		Challenge toPersist;
		if(c.getId()!=0){
		toPersist = challengeService.findOne(c.getId());
		}
		else{
			toPersist=challengeService.create(c.getEvent());
			
		}
		toPersist.setDescription(c.getDescription());
		toPersist.setResult(c.getResult());
		toPersist.setTitle(c.getTitle());
		
		challengeService.save(toPersist);
		result = new ModelAndView("redirect:/challenge/list.do?eventId="+toPersist.getEvent().getId());
		
		return result;
	}
}
